﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Mail;
using System.Net;


namespace ChurchDiary.Common
{
    public static class CommonFunction
    {
        public static string GetOrginalImagePath(string ImageName,string ImagePath)
        {
            string ImageOrginalPath = GetApplicationLink() + ImagePath + "/" + ImageName;
            ImageOrginalPath = ImageOrginalPath.Remove(ImageOrginalPath.IndexOf('~'), 1);
            return ImageOrginalPath;
        }
        public static string GetApplicationLink()
        {
            string applicationLink = "";
            Uri uri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri);
            if (System.Web.HttpContext.Current.Request.IsLocal)
            {
                applicationLink = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port; ;
            }
            else
            {
                applicationLink = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
            }
            return applicationLink;
        }
        public static string UppercaseFirst(string s)
        {
            // Check for empty string.
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            // Return char and concat substring.
            return char.ToUpper(s[0]) + s.Substring(1);
        }
        public static void SendMail(string toAddress, string MailContent)
        {
            try
            {
                string IsLocal = System.Web.HttpContext.Current.Request.IsLocal.ToString();
                if (IsLocal == "True")
                {
                    MailMessage mail = new MailMessage();
                    mail.From = new MailAddress("justinit21@gmail.com");
                    mail.Subject = "Message from website";
                    mail.IsBodyHtml = true;
                    mail.Body = MailContent;
                    mail.Bcc.Add("justinit21@gmail.com");
                    mail.To.Add(new MailAddress(toAddress));
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential net = new NetworkCredential();
                    net.UserName = "justinit21";
                    net.Password = "9965274787sam";
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = net;
                    smtp.Port = 587;
                    smtp.Send(mail);
                }
                else
                {
                    MailMessage message = new MailMessage();
                    SmtpClient smtpClient = new SmtpClient();
                    string msg = string.Empty;
                    MailAddress fromAddress = new MailAddress("support@greendotnetworld.in");
                    message.From = fromAddress;
                    message.Bcc.Add("justinit21@gmail.com");
                    message.Subject = "Message from website";
                    message.IsBodyHtml = true;
                    message.Body = MailContent;
                    smtpClient.Host = "relay-hosting.secureserver.net"; //-- Donot change.
                    smtpClient.Port = 25; //--- Donot change
                    smtpClient.EnableSsl = false;//--- Donot change
                    smtpClient.UseDefaultCredentials = true;
                    smtpClient.Credentials = new System.Net.NetworkCredential("support@greendotnetworld.in", "9965274787_sam");
                    smtpClient.Send(message);
                }
            }
            catch (Exception ex)
            {
            }
        }
        public static string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }
        public static StringBuilder GetEmptyWedget()
        {
            StringBuilder sbEmpty = new StringBuilder();
            sbEmpty.Append("<div class=\"contractionblock\">");
            sbEmpty.Append("<div class=\"icons\"><i class=\"fa fa-recycle\"></i></div>");
            sbEmpty.Append("<div class=\"Pleasing\">No Data Available</div>");
            sbEmpty.Append("</div>");
            return sbEmpty;
        }
        public static string Createfolder(string folderName)
        {
            string Path = System.Web.HttpContext.Current.Server.MapPath(folderName);
            bool exists = System.IO.Directory.Exists(Path);
            if (!exists)
            {
                System.IO.Directory.CreateDirectory(Path);
            }
            return Path;
        }
        public static string UploadImage(string path, string baseUrl)
        {
            if (!string.IsNullOrEmpty(baseUrl))
            {
                if (baseUrl.IndexOf(',') > 0)
                {
                    path = Createfolder(path);
                    string[] baseUrlsplit = baseUrl.Split(',');
                    baseUrl = baseUrlsplit[1];
                    string x = baseUrl;
                    byte[] imageBytes = Convert.FromBase64String(x);
                    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
                    ms.Write(imageBytes, 0, imageBytes.Length);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                    string FileName = Guid.NewGuid() + ".png";
                    path = path + '/' + FileName;
                    image.Save(path, System.Drawing.Imaging.ImageFormat.Png);
                    return FileName;
                }
                else
                {
                    return baseUrl;
                }
            }
            else
            {
                return baseUrl;
            }
        }
        public static void SendXmlMails()
        {
            try
            {
                string IsLocal = System.Web.HttpContext.Current.Request.IsLocal.ToString();
                if (IsLocal != "True")
                {
                    MailMessage message = new MailMessage();
                    SmtpClient smtpClient = new SmtpClient();
                    string msg = string.Empty;
                    MailAddress fromAddress = new MailAddress("donotreplay@fgpc.in");
                    message.From = fromAddress;
                    message.Bcc.Add("justinit21@gmail.com");
                    message.To.Add("justinit21@gmail.com");
                    message.Subject = "Message from website";
                    message.IsBodyHtml = true;
                    message.Body = "test";
                    smtpClient.Host = "mail.fgpc.in"; //-- Donot change.
                    smtpClient.Port = 25; //--- Donot change
                    smtpClient.EnableSsl = false;//--- Donot change
                    smtpClient.UseDefaultCredentials = true;
                    smtpClient.Credentials = new System.Net.NetworkCredential("donotreplay@fgpc.in", "Ju$t!n$s@m123");
                    smtpClient.Send(message);
                }
                else
                {
                    MailMessage mail = new MailMessage();
                    mail.From = new MailAddress("justinit21@gmail.com");
                    mail.Subject = "Message from website";
                    mail.IsBodyHtml = true;
                    mail.Body = "";
                    mail.Bcc.Add("justinit21@gmail.com");
                    mail.To.Add(new MailAddress("justinit21@gmail.com"));
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential net = new NetworkCredential();
                    net.UserName = "justinit21";
                    net.Password = "9965274787sam";
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = net;
                    smtp.Port = 587;
                    smtp.Send(mail);
                }
            }
            catch (Exception ex)
            {
            }
        }
        public static string GetCountryById(string ipAddress)
        {
            return "";
        }
    }
}
